package LookAndFeel.Gui.Imag;


import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

/**
 *
 * @author killan
 */
public class JPanelImage extends JPanel {

    private Image image = null;
    private Icon icon;

    /** Constructores **/
    public JPanelImage() {
    }

    public JPanelImage(String nombreImagen) {
        if (nombreImagen != null) {
            image = new ImageIcon(getClass().getResource(nombreImagen)).getImage();
        }
        repaint();
    }

    public JPanelImage(Image imagenInicial) {
        if (imagenInicial != null) {
            image = imagenInicial;
        }
        repaint();
    }

    public void setBackground(File file) throws IOException {
        if (file == null) {
            image = null;
        } else {
            image = ImageIO.read(file);
        }
        repaint();
    }

    public void setBackground(URL url) throws IOException {
        if (url == null) {
            image = null;
        } else {
            image = ImageIO.read(url);
        }
        repaint();
    }

    public void setBackground(String path) throws IOException {
        if (path == null) {
            image = null;
        } else {
            image = ImageIO.read(new File(path));
        }
        repaint();
    }

    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
        repaint();
    }

    public Icon getIcon() {
        return icon;
    }

    public void setIcon(Icon icon) {
        this.icon = icon;
        setImage(((ImageIcon) icon).getImage());
        repaint();
    }

    @Override
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

        if (getImage() != null) {
            g2.drawImage(getImage(), 0, 0, getWidth(), getHeight(), null);
        }
    }
    /*    public void setImage(Image im) {
    imagen = im;
    }
     */
    /*  @Override
    public void paint(Graphics g) {
    g.setColor(getBackground());
    g.fillRect(0, 0, getWidth(), getHeight());
    if (imagen != null) {
    g.drawImage(imagen, 0, 0, null);
    }
    Component c;
    for (int i = 0; i < getComponentCount(); i++) {
    c = getComponent(i);
    g.translate(c.getX(), c.getY());
    c.print(g);
    g.translate(-c.getX(), -c.getY());
    }
    }*/
}
